/*
 *   The class XmlReader is for loading the referred XML 
 *   file and then parsing it to Document form
 *   
 *       done by Lee in 2013/1/29
 */
package lee.base.xml;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import lee.base.constant.Symbol;
import lee.base.constant.XmlAttribute;
import lee.base.structure.MyHashTree;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XmlReader{
	
	private MyHashTree myTree = null;
	/**
	 * The constructor of class XmlReader
	 * @param fileName : the path of file - String
	 */
	public XmlReader(String fileName) {
		this.myTree = this._001_parseXmlFile(fileName);
	}
	/**
     * Do parsing the specific XML file
     * @param fileName : the path of file - String
     * @return MyHashTree
     */
	private MyHashTree _001_parseXmlFile(String fileName) {
		DocumentBuilderFactory factory = null;
		DocumentBuilder builder = null;
		Document doc = null;
		//read file
		File readFile = new File(fileName);
		//start to parse XML file
		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(readFile);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		MyHashTree tree = new MyHashTree(XmlAttribute.SYMBOL_REF_VARIABLE + "", XmlAttribute.VARIABLE_POINT + "");
		tree.ACT001_storeFromDocument(doc);
		return tree;
	}
	/**
	 * get instance variable - myTree
	 * @return MyHashTree
	 */
	public MyHashTree GET001_MyHashTree() {
		return this.myTree;
	}
}
